package com.baitai.web.controller.system;

import com.baitai.common.core.domain.AjaxResult;
import com.baitai.common.utils.SecurityUtils;
import com.baitai.common.utils.StringUtils;
import com.baitai.system.domain.SysConfig;
import com.baitai.system.service.IIndexService;
import com.baitai.system.service.ISysConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/system/index")
public class IndexController {

    @Autowired
    private IIndexService iIndexService;


    /**
     * 查询任务下发列表
     */
    @GetMapping("/userNumber")
    public AjaxResult userNumber()
    {
        Long userId = SecurityUtils.getUserId();
        Map<String,String> data = iIndexService.getUserNumber(userId);
        return AjaxResult.success(data);
    }

    /**
     * 查询任务下发列表
     */
    @GetMapping("/yesterdayNumber")
    public AjaxResult yesterdayNumber()
    {
        Long userId = SecurityUtils.getUserId();
        Map<String,String> data = iIndexService.yesterdayNumber(userId);
        return AjaxResult.success(data);
    }    /**
     * 查询任务下发列表
     */
    @GetMapping("/taskNumber")
    public AjaxResult taskNumber()
    {
        Long userId = SecurityUtils.getUserId();
        Map<String,String> data = iIndexService.taskNumber(userId);
        return AjaxResult.success(data);
    }

    @GetMapping("/getLineChart")
    public AjaxResult getLineChart(String type)
    {

        Map<LocalDate, String> data = iIndexService.getLineChart(type,SecurityUtils.getUserId());
        return AjaxResult.success(data);
    }

    @GetMapping("/getUserStatistics")
    public AjaxResult getUserStatistics()
    {
        Long userId = SecurityUtils.getUserId();
        Map<String, String> data = iIndexService.getUserStatistics(userId);
        return AjaxResult.success(data);
    }

    @GetMapping("/isAdmin")
    public AjaxResult isAdmin()
    {
        Long userId = SecurityUtils.getUserId();
        return AjaxResult.success("",iIndexService.isAdmin(userId));
    }
    @GetMapping("/getRatio")
    public AjaxResult getRatio(String configKey)
    {
        String configValue = iIndexService.selectConfigByKey(configKey);
        if(StringUtils.isNotEmpty(configValue)){
            return AjaxResult.success("",configValue);
        }else {
            return AjaxResult.success("比例为空");
        }
    }

    @PostMapping("/setRatio")
    public AjaxResult setRatio(@RequestBody SysConfig config)
    {
        SysConfig configValue = iIndexService.hasConfig(config.getConfigKey());
        config.setConfigType("N");
        if(configValue!=null){
            iIndexService.updateConfigByConfigKey(config);
            return AjaxResult.success("",configValue);
        }else {
            iIndexService.insertConfig(config);
            return AjaxResult.success();
        }
    }
}
